var api = require('../../../../api.js');
var app = getApp();
Page({
    data: {

        currentTab: 0, //预设当前项的值
        scrollLeft: 0, //tab标题的滚动条位置
        userimg: 'https://ss0.bdstatic.com/70cFuHSh_Q1YnxGkpoWK1HF6hhy/it/u=3774582627,2752870295&fm=26&gp=0.jpg', //用户头像
        gender: ['男', '女', '其他'],
        carlist: ['宝马', '现代', '比亚迪', '大众', '哈弗', '奥迪'],
        label: ['艺术活动', '运动健身', '旅游摄影', '购物', '手工艺制作', '表演', '美食', '时尚数码', '大自然生态'],
        cx: ['Mercedes-Benz-进口', 'Mdercedes-Benz-国产', 'BMW-进口', 'BMW国产', 'Audi-进口', 'Audi-国产', 'Land Rover-进口', 'Land Rover-国产', 'Porsche-保时捷', 'Maserati-玛莎拉蒂', '其他-进口', '其他-国产'],
        selectIndex: [],
        xg: ['物美价廉', '品质至上'],
        xgselect: [],
        labelselect: [],

    },
    select: function(e) {
        let selectIndex = this.data.selectIndex;
        let index = e.currentTarget.dataset.index;
        selectIndex[index] = !selectIndex[index];
        // console.log(selectIndex)
        this.setData({
            selectIndex: selectIndex
        })
    },
    //喜爱活动  
    selecth: function(e) {
        let labelselect = this.data.labelselect;
        let index = e.currentTarget.dataset.index;
        labelselect[index] = !labelselect[index];
        console.log(labelselect)
        this.setData({
            labelselect: labelselect
        })
    },
    //消费习惯
    selectx: function(e) {
        console.log(e);
        let xgselect = this.data.xgselect;
        let index = e.currentTarget.dataset.index;
        xgselect[index] = !xgselect[index];
        console.log(xgselect);
        this.setData({
            xgselect: xgselect
        })
    },
    //姓名
    bindname: function(e) {
        this.setData({
            name: e.detail.value
        })
    },

    //车牌号
    platenumber: function(e) {
        this.setData({
            plate: e.detail.value
        })
    },

    // 选择性别
    choose: function(e) {
        console.log(e.detail.value)
        let that = this;
        that.setData({
            gd_index: e.detail.value
        })
        console.log(that.data.gender[e.detail.value])
    },
    // 选择生日
    choose_day: function(e) {
        console.log(e.detail.value)
        let that = this;
        that.setData({
            data: e.detail.value
        })
    },
    // 选择车型
    choose_car: function(e) {
        console.log(e.detail.value)
        let that = this;
        that.setData({
            car_index: e.detail.value
        })

    },
    // 联系方式提交表单
    contact_way: function(e) {
        console.log(e)
        var tel = e.detail.value.tel;
        var home_address = e.detail.value.address;
        var work_address = e.detail.value.work_add;
        if (tel == '' || tel == undefined) {
            wx.showToast({
                title: '手机号码不可以为空',
                icon: 'none',
                duration: 2e3
            })
            return false;
        }

        if (home_address == '' || home_address == undefined) {
            wx.showToast({
                title: '家庭住址不可以为空',
                icon: 'none',
                duration: 2e3
            })
            return false;
        }
        if (work_address == '' || work_address == undefined) {
            wx.showToast({
                title: '工作住址不可以为空',
                icon: 'none',
                duration: 2e3
            })
            return false;
        }
        app.request({
            url: api.user.contact,
            data: {
                tel: tel,
                home_address: home_address,
                work_address: work_address,
            },
            success: function(e) {
                console.log(e);
                wx.showToast({
                    title: e.msg,
                    duration: 2e3
                })
            }
        });


    },

    //保存个人资料
    keepdata: function(e) {
        let tath = this;
        var name = tath.data.name;
        var sex = tath.data.gd_index;
        var birthday = tath.data.data;

        var car_number = tath.data.plate;
        // ======================================================

        var cp = /(^[\u4E00-\u9FA5]{1}[A-Z0-9]{6}$)|(^[A-Z]{2}[A-Z0-9]{2}[A-Z0-9\u4E00-\u9FA5]{1}[A-Z0-9]{4}$)|(^[\u4E00-\u9FA5]{1}[A-Z0-9]{5}[挂学警军港澳]{1}$)|(^[A-Z]{2}[0-9]{5}$)|(^(08|38){1}[A-Z0-9]{4}[A-Z0-9挂学警军港澳]{1}$)/.test(car_number);
        console.log(cp)
        console.log("cp))------------------------------------------------")
        if(cp==false){
            wx.showModal({
                title: '注意',
                content: '您的车牌号填写有误，请重新填写（填写信息请勿携带空格）',
                showCancel: true,
            })
            return false;
        }
        // =====================================================

        var cx = this.data.cx;
        var selectIndex = this.data.selectIndex;
        var cxdata = [];
        for (var i = 0; i < cx.length; i++) {
            if (selectIndex[i]) {
                cxdata.push(cx[i]);
            }
        }
        console.log(cxdata);
        var car_model = cxdata.join(',');


        app.request({
            url: api.user.information,
            data: {
                xm_name: name,
                sex: sex,
                birthday: birthday,
                car_model: car_model,
                car_number: car_number,
            },
            success: function(e) {
                console.log(e);
                wx.showToast({
                    title: e.msg,
                    duration: 2e3
                })
            }
        });
    },

    onReady: function() {
       
    },









    //个人描述
    describe: function(e) {
        //喜爱的活动
        var label = this.data.label;
        var labelselect = this.data.labelselect;
        var xadhd = [];
        for (var i = 0; i < label.length; i++) {
            if (labelselect[i]) {
                xadhd.push(label[i]);
            }
        }
        //消费的习惯
        var xg = this.data.xg;
        var xgselect = this.data.xgselect;
        var xfdxg = [];
        for (var i = 0; i < xg.length; i++) {
            if (xgselect[i]) {
                xfdxg.push(xg[i]);
            }
        }

        var like_activity = xadhd.join(',');
        var habits = xfdxg.join(',');

        app.request({
            url: api.user.describe,
            data: {
                like_activity: like_activity,
                habits: habits,
            },
            success: function(e) {
                console.log(e);
                wx.showToast({
                    title: e.msg,
                    duration: 2e3
                })
            }
        });


    },

    // 滚动切换标签样式
    switchTab: function(e) {
        this.setData({
            currentTab: e.detail.current
        });
        this.checkCor();
    },
    // 点击标题切换当前页时改变样式
    swichNav: function(e) {
        var cur = e.target.dataset.current;
        if (this.data.currentTaB == cur) {
            return false;
        } else {
            this.setData({
                currentTab: cur
            })
        }
    },
    //判断当前滚动超过一屏时，设置tab标题滚动条。
    checkCor: function() {
        if (this.data.currentTab > 4) {
            this.setData({
                scrollLeft: 300
            })
        } else {
            this.setData({
                scrollLeft: 0
            })
        }
    },
    onLoad: function() {
        var that = this;
        //  高度自适应
        console.log(wx.getSystemInfoSync().windowHeight)
        that.setData({
            winH: wx.getSystemInfoSync().windowHeight,
        })

        that.loadData();
    },

    loadData: function(e) {
        var a = this;
        var t = wx.getStorageSync("pages_user_user");
        t && a.setData(t), app.request({
            url: api.user.index,
            success: function(e) {
                var cxx = e.data.user_info.car_model; //车型
                var hd = e.data.user_info.like_activity; //喜爱的活动
                var xg = e.data.user_info.habits; //消费习惯
                var loddata = cxx.split(","); //这个就是返回的数据
                var _hd = hd.split(","); //这个就是返回的数据
                var _xg = xg.split(","); //这个就是返回的数据
                var cx = a.data.cx;
                var selectIndex = a.data.selectIndex;
                var _label = a.data.label;
                var labelselect = a.data.labelselect;
                var xg = a.data.xg;
                var xgselect = a.data.xgselect;
                for (var j = 0; j < cx.length; j++) {
                    for (var i = 0; i < loddata.length; i++) {
                        if (cx[j] == loddata[i]) {
                            selectIndex[j] = true;
                        }
                    }
                }

                for (var j = 0; j < _label.length; j++) {
                    for (var i = 0; i < _hd.length; i++) {
                        if (_label[j] == _hd[i]) {
                            labelselect[j] = true;
                        }
                    }
                }

                for (var j = 0; j < xg.length; j++) {
                    for (var i = 0; i < _xg.length; i++) {
                        if (xg[j] == _xg[i]) {
                            xgselect[j] = true;
                        }
                    }
                }

                a.setData({
                        gd_index: e.data.user_info.sex,
                        data: e.data.user_info.birthday,
                        selectIndex: selectIndex,
                        labelselect: labelselect,
                        xgselect: xgselect
                    }),
                    0 == e.code && (a.setData(e.data), wx.setStorageSync("pages_user_user", e.data),
                        wx.setStorageSync("share_setting", e.data.share_setting), wx.setStorageSync("user_info", e.data.user_info),
                        console.log(e.data));
            }
        });
    },


    footerTap: app.footerTap
})